*	Illiberal Communication and Election Intervention During the Refugee Crisis in GermanyIlliberal Communication and Election Intervention During the Refugee Crisis in Germany

*	Perspectives on Poltics Rep file for Figures 4 and 5ab

*	STATA DTA STATA Analysis folder

*	Overview:
*	_________

*	30 day period for windows

*	analyze.do  -> is this file

*	there are two .do files for graphs,

*	graph_es.do and one is this ->  graph_es uses ar.csv generated here (req. addl manual processing, by first opening RT csv and then pasting the Sputnk csv under while omitting the header and saving the ar file as ar.csv) 


*	Requires:

*	main_data2.tsv <-- Imports Dennis data which is one per day for long time

* 	asyl.dta  <-- imports EU mo asylum app data

*	bild_news_relevant-migrant-news
*	faz_news_relevant-migrant-news
*	rt_news_relevant-migrant-news
*	rt_relevant-migrant-news
*	sueddeutsche_relevant-migrant-news
*	taz_relevant-migrant-news
*	welt_relevant-migrant-news
*    
*   ***************************************************


*	This file: takes in (STATA, one per day, t n art and t n words) transformed data for refugees

*	.generates data for event-study and for t-test

*	.generates overview graph for dist of refugee stories


set more off /* this command just keeps STATA from pausing after each screen of output */

drop _all

import delimited using main_data2.tsv, delim(tab)

drop v1
	
replace date=strlower(date)
rename date datestr
gen date=date(datestr,"DMY",2050)                  

gen daten=date

format date %td
gen month = mofd(date)
generate year=year(date)

sort date

gen date1=date(datestr,"DMY",2050)

*	some dates repeat, drop later

egen uni=tag(date)
tab uni
assert uni==1
drop if uni==0
drop uni

tsset date

sort date

order date month year date1

keep date-datestr daten mh17event1-mh17url6

gen month2 = mofd(date1)


*	This is mo data on refugee registrations

merge m:1 month using asyl.dta

drop if (_m==2)

drop _m


foreach v in bild_news_relevant-migrant-news.dta faz_news_relevant-migrant-news.dta rt_news_relevant-migrant-news.dta sputnik_relevant-migrant-news.dta sueddeutsche_relevant-migrant-news.dta taz_relevant-migrant-news.dta welt_relevant-migrant-news.dta  {
	merge 1:1 date using "`v'", update
	drop _m
}


*	gen 0's for Russian

foreach v of varlist rtw rtn sputnikw sputnikn {

	replace `v'=0 if `v'==. & tin(15nov2014, 31dec2018)

}


*	gen 0's for German


foreach v of varlist bildw bildn fazw fazn szw szn tazw tazn weltn weltw  {

	replace `v'=0 if `v'==. & tin(1jan2014, 31dec2018)

}


* Words and Stories Total By Mo

foreach v of varlist bildw bildn  rtw rtn fazw fazn sputnikw sputnikn szw szn tazw tazn weltn weltw {

	bysort month: egen `v't=total(`v') if `v'!=.

}


*	
* 	GEN WINDOWS
*


*	30 day +/- periodin middle -> combine events where difference is under 2 mos

capture drop  week

gen week = wofd(date)

move week month

*	These three elections feature enough of a window [they need not be combined]

capture drop Hamburg0215 

capture drop Bremen0515

capture drop BWRPSA0316 

local states "Hamburg0215 Bremen0515 BWRPSA0316"

local dates "20134 20218 20526"

local n: word count `states'

forvalues i = 1/`n' {
	local a: word `i' of `states'
	local b: word `i' of `dates'
* 	gen `a'=0 if abs(date1-`b')<56 
* 	replace `a'=1 if abs(date1-`b')<28 
 	gen `a'=0 if abs(date1-`b')<60 
 	replace `a'=1 if abs(date1-`b')<30 

	
	}

 
*	Some elections need to be merged as too close:	MV0916 Berlin0916 -> MVBerlin0916 /14 days/
 
capture drop MVBerlin0916 

capture drop Berlin0916

capture drop MV0916

local dates "20701 20715"

local states "MV0916 Berlin0916"

local n: word count `states'

forvalues i = 1/`n' {
	local a: word `i' of `states'
	local b: word `i' of `dates'
* 	gen `a'=0 if abs(date1-`b')<56 
* 	replace `a'=1 if abs(date1-`b')<28 
 	gen `a'=0 if abs(date1-`b')<60 
 	replace `a'=1 if abs(date1-`b')<30 

	
	}

	
*	COMBINE	
	
gen MVBerlin0916=0 if (MV0916!=. |  Berlin0916!=.)

replace MVBerlin0916=1 if (MV0916==1 |  Berlin0916==1)
	
 
*	Some elections need to be merged as too close:	Saar0317 SH0517 RW0517 -> SaarSHRH030517 /-42- d -7- d -> 49 days/ 15d or mo around ele would change that

capture drop SaarSHRH030517 
 
capture drop Saar0317

capture drop SH0517 

capture drop RW0517 


local dates "20904 20946 20953"

local states "Saar0317 SH0517 RW0517"

local n: word count `states'

forvalues i = 1/`n' {
	local a: word `i' of `states'
	local b: word `i' of `dates'
 	gen `a'=0 if abs(date1-`b')<60 
 	replace `a'=1 if abs(date1-`b')<30 
	
	}

*	COMBINE	

	
gen SaarSHRH030517=0 if (Saar0317!=. |  SH0517!=. |  RW0517!=.)

replace SaarSHRH030517=1 if (Saar0317==1 |  SH0517==1 | RW0517==1)
	

	*	Some elections need to be merged as too close:	Bundestag0917 LoSa1016 -> BundestagLoSa091017 /-21 d-/
	

capture drop BundestagLoSa091017
	
capture drop Bundestag0917 

capture drop LoSa1016

local dates "21086 21107"

local states "Bundestag0917 LoSa1016"

local n: word count `states'

forvalues i = 1/`n' {
	local a: word `i' of `states'
	local b: word `i' of `dates'
* 	gen `a'=0 if abs(date1-`b')<56 
* 	replace `a'=1 if abs(date1-`b')<28 
 	gen `a'=0 if abs(date1-`b')<60 
 	replace `a'=1 if abs(date1-`b')<30 

	
	}

* Combine	
	
gen BundestagLoSa091017=0 if (Bundestag0917!=. |  LoSa1016!=.)

replace BundestagLoSa091017=1 if (Bundestag0917==1 |  LoSa1016==1)


*	Some elections need to be merged as too close:	Bay1018 Hes1018 -> BundestagLoSa091017 / -13 d-/
	
	
capture drop BayHes1018 

capture drop Bay1018

capture drop Hes1018

	
local dates "21472 21485"

local states "Bay1018 Hes1018"

local n: word count `states'

forvalues i = 1/`n' {
	local a: word `i' of `states'
	local b: word `i' of `dates'
* 	gen `a'=0 if abs(date1-`b')<56 
* 	replace `a'=1 if abs(date1-`b')<28 
 	gen `a'=0 if abs(date1-`b')<60 
 	replace `a'=1 if abs(date1-`b')<30 

	
	}
	
* Combine	

	
gen BayHes1018=0 if (Bay1018!=. |  Hes1018!=.)

replace BayHes1018=1 if (Bay1018==1 |  Hes1018==1)
	
 
* T-TEST
 
local states "Hamburg0215 Bremen0515 BWRPSA0316 MVBerlin0916 SaarSHRH030517 BundestagLoSa091017 BayHes1018"

local outlets "bildn rtn sputnikn fazn szn tazn weltn"

local n: word count `states'

local m: word count `outlets'

forvalues i = 1/`m' 					{
	local a: word `i' of `outlets'
	forvalues j = 1/`n' 				{
		local b: word `j' of `states'
		di "Equality of means, refugee-mentions in `a' within 3 weeks l and r of `b'"
		ttest `a', by(`b')
	}	
}






*** Event Study * Abnormal Interest in Refugees 

 
*  ESTIMATION WINDOWS AND EVENT WINDOWS .... STATE 0 and 1

capture drop HH	HB BW MV SL DE BY dif

capture drop dif=.

gen dif=.

*	states how I call them in graph

local states "HH HB BW MV SL DE BY"

*	election windows for diff state elections 0/1

local windows "Hamburg0215 Bremen0515 BWRPSA0316 MVBerlin0916 SaarSHRH030517 BundestagLoSa091017 BayHes1018"

*	a date in the middle of state elections window 0/1

local dates "20134 20218 20526 20708 20928 21096 21478"

*	duration of event window

local days "59 59 59 73 108 80 72"

local n: word count `states'

forvalues i = 1/`n' {
	local a: word `i' of `states'
	local b: word `i' of `dates'
	local c: word `i' of `windows'
	local d: word `i' of `days'
 	gen `a'=0 if `c'==0 
 	replace `a'=1 if `c'==1 
	replace dif=0 if abs(date1-`b')==0
 }


  
 
*** END Windows

  

*	RT EVENT STUDY
  

capture drop abnormal_return
gen  abnormal_return=.

capture drop test
gen test=.
 
capture drop cumulative_abnormal_return1
gen  cumulative_abnormal_return1=.

capture drop ar_sd1
gen  ar_sd1=.
 
capture drop ave_abnormal_return
*gen ave_abnormal_return=cumulative_abnormal_return1/83
gen ave_abnormal_return=.

capture drop se
*gen se=ar_sd1/(sqrt(83))
gen se=.
 
  
local states "HH HB BW MV SL DE BY"

local days "59 59 59 73 108 80 72"

local n: word count `states'
 
local a "rtn"

forvalues i = 1/`n' {

	local b: word `i' of `states'
	local d: word `i' of `days'

	reg `a' bildn szn tazn weltn fazn asyl if `b'==0
	
	count if `i'==1
    if r(N) > 0 {
         outreg2 using "refele`a'.xls", ctitle(" `b'") title("`a'") replace
    }
    else {
		outreg2 using "refele`a'.xls", ctitle(" `b'") append
	}

	
	predict p if (`b'==0 | `b'==1)
	gen predicted_`a'=p if `b'==1
	replace abnormal_return=`a'-predicted_`a' if `b'==1
	egen cumulative_abnormal_return = sum(abnormal_return) if `b'==1
	egen ar_sd = sd(abnormal_return) if `b'==1

	*	number of event days d of days - change if different
	
	replace test =(1/sqrt(`d'))*(cumulative_abnormal_return/ar_sd) if test==.
	
	replace cumulative_abnormal_return1=cumulative_abnormal_return if cumulative_abnormal_return1==.

	replace ave_abnormal_return=cumulative_abnormal_return1/`d'
		
	replace ar_sd1 = ar_sd if ar_sd1==.	

	replace se=ar_sd1/(sqrt(`d'))
	
	capture drop p predicted_`a' cumulative_abnormal_return ar_sd

	
 }
 
outsheet HH HB BW MV SL DE BY cumulative_abnormal_return1 ar_sd1 test ave_abnormal_return se using ar_`a'.csv if dif==0, comma replace 
 
*	SPUTNIK EVENT STUDY
  
capture drop abnormal_return
gen  abnormal_return=.

capture drop test
gen test=.
 
capture drop cumulative_abnormal_return1
gen  cumulative_abnormal_return1=.

capture drop ar_sd1
gen  ar_sd1=.
 
 
capture drop ave_abnormal_return
*gen ave_abnormal_return=cumulative_abnormal_return1/83
gen ave_abnormal_return=.


capture drop se
*gen se=ar_sd1/(sqrt(83))
gen se=.
 
  
local states "HH HB BW MV SL DE BY"

local days "59 59 59 73 108 80 72"

local n: word count `states'

*	SPUTNIK -->
 
local a "sputnikn"

forvalues i = 1/`n' {

	local b: word `i' of `states'
	local d: word `i' of `days'

	reg `a' bildn szn tazn weltn fazn asyl if `b'==0
	
	count if `i'==1
    if r(N) > 0 {
         outreg2 using "refele`a'.xls", ctitle(" `b'") title("`a'") replace
    }
    else {
		outreg2 using "refele`a'.xls", ctitle(" `b'") append
	}
	
	predict p if (`b'==0 | `b'==1)
	gen predicted_`a'=p if `b'==1
	replace abnormal_return=`a'-predicted_`a' if `b'==1
	egen cumulative_abnormal_return = sum(abnormal_return) if `b'==1
	egen ar_sd = sd(abnormal_return) if `b'==1

	*	number of event days d of days - change if different
	
	replace test =(1/sqrt(`d'))*(cumulative_abnormal_return/ar_sd) if test==.
	
	replace cumulative_abnormal_return1=cumulative_abnormal_return if cumulative_abnormal_return1==.

	replace ave_abnormal_return=cumulative_abnormal_return1/`d'
		
	replace ar_sd1 = ar_sd if ar_sd1==.	

	replace se=ar_sd1/(sqrt(`d'))
	
	capture drop p predicted_`a' cumulative_abnormal_return ar_sd

	
 }
 
outsheet HH HB BW MV SL DE BY cumulative_abnormal_return1 ar_sd1 test ave_abnormal_return se using ar_`a'.csv if dif==0, comma replace 


 
*** end EVENTSTUDY 
 

 
 
 
 
 
 
 
* 	Overview graph:

*	some dates repeat, drop later

*	weeks

capture drop uni
egen uni=tag(month)
drop if uni==0
drop uni
tsset date
sort date
format month %tmCY
keep if tin(1jan2014, 31dec2018)

capture drop lnasyl
gen lnasyl=asyl/200

set scheme s2mono

graph twoway scatter rtnt month, msymbol(o) legend(label(1 "RT") cols(3)) || scatter sputniknt month, msymbol(d) legend(label(2 "Sputnik")) || line lnasyl month, legend(label(3 "# asyl reg")) graphregion(color(white)) ytitle("") xtitle("") scale(.85) aspectratio(.5) tline(2017m9, lpattern(solid) lcolor(gs10)) tline(2014m8 2014m9 2015m2 2015m5 2016m3 2016m9 2017m3 2017m5 2017m10 2018m10, lpattern(dash) lcolor(gs15)) ylabel(,nogrid) tlabel(2014m1 2015m1 2016m1 2017m1 2018m1, format(%tmCY)) text(500 660.5 "HH", color(black) placement(west)) text(500 663.75 "HB", color(black) placement(west))  text(500 673.5 "BW", color(black) placement(west)) text(500 679.5 "MV", color(black) placement(west)) text(500 685.2 "SL", color(black) placement(west)) text(500 691.5 "DE", color(black) placement(west)) text(500 704.5 "BY", color(black) placement(west))

graph export Figure5a.pdf, replace

graph twoway scatter taznt month, msymbol(oh) legend(label(1 "TAZ") cols(6)) || scatter weltnt month, msymbol(s) legend(label(2 "Welt")) || scatter bildnt month, msymbol(smplus) legend(label(3 "Bild")) || scatter faznt month, msymbol(x) legend(label(4 "FAZ")) || scatter sznt month, msymbol(sh) legend(label(5 "SZ")) || line lnasyl month, lpattern(shortdash) lcolor(gs5) legend(label(6 "# asyl reg")) graphregion(color(white)) ytitle("") xtitle("") scale(.85) aspectratio(.5) tline(2017m9, lpattern(solid) lcolor(gs10)) tline(2014m8 2014m9 2015m2 2015m5 2016m3 2016m9 2017m3 2017m5 2017m10 2018m10, lpattern(dash) lcolor(gs15)) ylabel(,nogrid) tlabel(2014m1 2015m1 2016m1 2017m1 2018m1, format(%tmCY)) text(500 660.5 "HH", color(black) placement(west)) text(500 663.75 "HB", color(black) placement(west))  text(500 673.5 "BW", color(black) placement(west)) text(500 679.5 "MV", color(black) placement(west)) text(500 685.2 "SL", color(black) placement(west)) text(500 691.5 "DE", color(black) placement(west)) text(500 704.5 "BY", color(black) placement(west))

graph export Figure5b.pdf, replace

